package spring.config;

import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
import org.springframework.orm.hibernate4.support.OpenSessionInViewFilter;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class WebInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
    	//和hibernate相关的过滤器，为了防止session提前关闭。
    	servletContext.addFilter("hibernateFilter", OpenSessionInViewFilter.class).addMappingForUrlPatterns(null, false, "/*");  
    	
    	//创建Spring应用程序上下文。这里关键，未来所有的对象都放在这里
    	AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();  
    	//注册数据源
        root.register(DataSourceConfig.class);
        //Spring的Web监听器
        servletContext.addListener(new ContextLoaderListener(root));
        //追加一个过滤器，该过滤将过滤所有请求。
        Dynamic filter=servletContext.addFilter("struts2", new StrutsPrepareAndExecuteFilter());
        filter.addMappingForUrlPatterns(null,false,"/*");
    }  
}
